package hzst.android.form;

import hzst.android.Constants;
import hzst.android.entity.Accessory;
import hzst.android.entity.BaseUser;
import hzst.android.form.info.AccessoryViewInfo;
import hzst.android.form.info.BaseViewInfo;
import hzst.android.form.info.EditViewInfo;
import hzst.android.form.info.FormViewInfo;
import hzst.android.form.info.SelectViewInfo;
import hzst.android.form.info.SelectableViewInfo;
import hzst.android.util.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 解析控件json，用于后续动态创建表单。
 * @author wt
 *
 */
public class ViewInfoProcessor {
	private Map<String, String> parameters;
	private Map<String, String> temp;
	
	public Map<String, String> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters;
	}

	/*
	 * json中的字段
	 */
	public static final String FIELD_TYPE = "viewType";//控件类型
	public static final String FIELD_NAME = "name";//字段对应的名称(单元格的标题)
	public static final String FIELD_SUBMIT_ITEMS = "submitItemst";//提交字段的值，可能有多个，用","隔开
	public static final String FIELD_MUST_FILL = "mustFill";//是否必填(1/0)
	public static final String FIELD_SPECIAL = "special";//是否有特殊操作(1/0)
	public static final String FIELD_LIST_ITEMS = "listItem";//下拉框、多选框等数据
	public static final String FIELD_ACCESSORY_LIST = "accessoryList";//附件
	public static final String FIELD_SELECT_SOURCE = "selectSource";//选择的数据源类型
	public static final String FIELD_SELECT_TYPE = "selectType";//选择的数据源是否多选、是否需要传name和id
	
	public List<BaseViewInfo> getViewInfo(String json,BaseUser user){
		List<BaseViewInfo> viewList = new ArrayList<>();
		temp = new HashMap<>();
		try {
			JSONArray array = new JSONArray(json.trim());
			int parameterCount = 0;
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				BaseViewInfo info = new BaseViewInfo();
				String viewType = obj.getString(FIELD_TYPE);
				
				switch (viewType) {
				case ViewCreator.VIEW_TYPE_TASK://任务相关数据(默认放在json数据最前面，会先被解析)
					parameterCount ++;
					temp.put(obj.getString("fieldName"), obj.getString("value"));
					continue;
				case ViewCreator.VIEW_TYPE_TABLE:
					if(parameters == null){
						parameters = new HashMap<>();
						parameters.putAll(temp);
					}
					//递归解析从表数据
					info.setSlaveTableInfos(getViewInfo(obj.getString("value"), user));
					//添加从表任务相关数据
					info.setParameters(temp);
					break;
				case ViewCreator.VIEW_TYPE_SP:
				case ViewCreator.VIEW_TYPE_CG:
					info = new SelectableViewInfo();
					break;
				case ViewCreator.VIEW_TYPE_SV:
					info = new SelectViewInfo();
					((SelectViewInfo)info).setSelectSource(parseJsonElement(obj, FIELD_SELECT_SOURCE));
					((SelectViewInfo)info).setSelectType(obj.getString(FIELD_SELECT_TYPE));
					((SelectViewInfo)info).setSourceUrl(parseJsonElement(obj, "sourceUrl"));
				case ViewCreator.VIEW_TYPE_ET:
					if(!(info instanceof EditViewInfo)){
						info = new EditViewInfo();
					}
					((EditViewInfo)info).setIntputType(parseJsonElement(obj,"inputType"));
					try {
						((EditViewInfo)info).setMaxEms(Integer.valueOf(parseJsonElement(obj,"maxEms")));
					} catch (NumberFormatException e) {
						L.showLogInfo(L.TAG_EXCEPTION, e.toString());
					}
					break;
				case ViewCreator.VIEW_TYPE_AV:
					info.setVisible(true);
					info = new AccessoryViewInfo();
					((AccessoryViewInfo)info).setAccessories(parseAccessory(obj.getString(FIELD_ACCESSORY_LIST)));
					break;
				case ViewCreator.VIEW_TYPE_FV:
					info = new FormViewInfo(parseJsonElement(obj, "submitItemst"));
					break;
				}

				info.setMustFill(Constants.TRUE.equals(parseJsonElement(obj, "mustFill")));
				info.setName(parseJsonElement(obj, "name"));
				info.setSpecial(Constants.TRUE.equals(parseJsonElement(obj, "special")));
				info.setSubmitInfo(parseJsonElement(obj, "submitItemst"));
				info.setListInfo(parseJsonElement(obj, "listItem"));
				info.setSpecialValue(parseJsonElement(obj, "specialValue"));
				info.setReadOnly(Constants.TRUE.equals(parseJsonElement(obj, "ReadOnly")));
				info.setViewType(viewType);
				info.setIndex(i - parameterCount);
				
				info.setUser(user);
				
				viewList.add(info);
			}
			if(parameters == null){
				parameters = new HashMap<>();
				parameters.putAll(temp);
			}

		} catch (JSONException e) {
			L.showLogInfo(L.TAG_EXCEPTION, e.toString());
		}
		return viewList;
	}
	/**
	 * 
	 * @param obj
	 * @param name
	 * @return
	 */
	public static String parseJsonElement(JSONObject obj,String name){
		if(obj.has(name)){
			try {
				return obj.getString(name);
			} catch (JSONException e) {
				L.showLogInfo(L.TAG_EXCEPTION, e.toString());
				return "";
			}
		}
		return "";
	}
	/**
	 * 解析附件相关json
	 * @param json
	 * @return
	 */
	private List<Accessory> parseAccessory(String json){
		List<Accessory> accessories = new ArrayList<>();
		try {
			JSONArray array = new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				Accessory accessory = new Accessory();
				//"fileName":"","filePath":"","ownerId":","accessoryId":""
				accessory.setAccessoryId(obj.getString("accessoryId"));
				accessory.setAttachmentName(obj.getString("fileName"));
				accessory.setAttachmentUrl(obj.getString("filePath"));
				accessory.setOwnerId(obj.getString("ownerId"));
				accessories.add(accessory);
			}
		} catch (JSONException e) {
			L.showLogInfo(L.TAG_EXCEPTION, e.toString());
		}
		return accessories;
	}
}
